//
//  LoginAndRegisterView.m
//  Comma
//
//  Created by liuchang on 15/1/16.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LoginAndRegisterView.h"

@implementation LoginAndRegisterView

- (instancetype)initWithUpTextField:(NSString *)upPlaceholder DownTextField:(NSString *)downPlaceholder UPButton:(NSString *)upButtonTitle DownButton:(NSString *)downButtonTitle LeftButton:(NSString *)leftButtonTitle RightButton:(NSString *)rightButtonTitle
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        if (upPlaceholder != nil) {
            [self creatUpTextFieldWithPlaceholder:upPlaceholder];
        }
        if (downPlaceholder != nil) {
            [self creatDownTextFieldWithPlaceholder:downPlaceholder];
        }
        if (upButtonTitle != nil) {
            [self creatUpButtonWithTitle:upButtonTitle];
        }
        if (downButtonTitle != nil) {
            [self creatDownButtonWithTitle:downButtonTitle];
        }
        if (leftButtonTitle != nil) {
            [self creatLeftButtonWithTitle:leftButtonTitle];
        }
        if (rightButtonTitle != nil) {
            [self creatRightButtonWithTitle:rightButtonTitle];
        }
    }
    [self registerForKeyboardNotifications];
    self.viewDidChange = NO;
    return self;
}

- (void)creatUpTextFieldWithPlaceholder:(NSString *)text
{
    self.upTextField = [[UITextField alloc] initWithFrame:CGRectMake(16, 356, SCREEN_WIDTH - 32, 44)];
    [_upTextField setPlaceholder:text];
    _upTextField.delegate = self;
    [self addSubview:_upTextField];
}

- (void)creatDownTextFieldWithPlaceholder:(NSString *)text
{
    self.downTextField = [[UITextField alloc] initWithFrame:CGRectMake(16, 400, SCREEN_WIDTH - 32, 44)];
    [_downTextField setPlaceholder:text];
    _downTextField.delegate = self;
    [self addSubview:_downTextField];
}

- (void)creatUpButtonWithTitle:(NSString *)title
{
    self.upButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_upButton setFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 516, 100, 44)];
    [_upButton setTitle:title forState:UIControlStateNormal];
    [_upButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [_upButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_upButton];
}

- (void)creatDownButtonWithTitle:(NSString *)title
{
    self.downButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_downButton setFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 560, 100, 44)];
    [_downButton setTitle:title forState:UIControlStateNormal];
    [_downButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [_downButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_downButton];
}

- (void)creatLeftButtonWithTitle:(NSString *)title
{
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_leftButton setFrame:CGRectMake(16, 312, 58, 44)];
    [_leftButton setTitle:title forState:UIControlStateNormal];
    [_leftButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    [_leftButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [_leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_leftButton];
}

- (void)creatRightButtonWithTitle:(NSString *)title
{
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_rightButton setFrame:CGRectMake(SCREEN_WIDTH - 74, 312, 58, 44)];
    [_rightButton setTitle:title forState:UIControlStateNormal];
    [_rightButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    [_rightButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightButton];
}

- (void)upButtonAction:(id)sender
{
    
}

- (void)downButtonAction:(id)sender
{
    
}

- (void)leftButtonAction:(id)sender
{
    
}

- (void)rightButtonAction:(id)sender
{
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - 由键盘更改View高度
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification
{
    
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyBoardHeight = kbSize.height;
    self.keyBoardDidShow = YES;
    [self viewWillChangeByKeyBoard];
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.editingTextField = textField;
    if (_keyBoardDidShow) {
        [self viewWillChangeByKeyBoard];
    }
}

- (void)viewWillChangeByKeyBoard
{
    CGFloat differ = _editingTextField.frame.origin.y + _editingTextField.frame.size.height - SCREEN_HEIGHT + _keyBoardHeight;
    if (_viewDidChange) {
        if (differ < 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = NO;
            }];
        }
    }
    else {
        if (differ > 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self setFrame:CGRectMake(0, - differ, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = YES;
            }];
        }
    }
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    if (_viewDidChange) {
        [UIView animateWithDuration:0.1 animations:^{
            [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        } completion:^(BOOL finished) {
            self.viewDidChange = NO;
        }];
    }
    self.keyBoardDidShow = NO;
}

#pragma mark - 由键盘更改View高度END



/*创建豆号logo图片*/
- (void)createLogoImage
{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(91.5, 108, 191, 243)];
    [imgView setImage:[UIImage imageNamed:@"app2.0png-03.png"]];
    [self addSubview:imgView];
}

// 回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}



@end
